package com.cw.carwashclients.controller;

import com.cw.carwashclients.domain.Customers;
import com.cw.carwashclients.service.CustomersService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomersController extends BaseController {
    @Autowired
    private CustomersService customersService;

    /**
     * 分页查询客户列表
     * @param customers
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(Customers customers) {
        startPage();
        List<Customers> customersList = customersService.selectCustomersList(customers);
        return getDataTable(customersList);
    }

    /**
     * 获取客户详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Customers detail(@PathVariable(value = "id") Long id) {
        return customersService.selectCustomersById(id);
    }

    /**
     *  新增客户
     * @param customers
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Customers customers) {
        return toAjax(customersService.insertCustomers(customers));
    }

    /**
     * 更新客户
     * @param customers
     * @return
     */
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Customers customers) {
        return toAjax(customersService.updateCustomers(customers));
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        return toAjax(customersService.deleteCustomersById(id));
    }

    /**
     *  批量删除客户
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    public AjaxResult deleteByIds(@PathVariable Long[] ids) {
        return toAjax(customersService.deleteCustomersByIds(ids));
    }

    /**
     *  导出客户
     * @param response
     * @param customers
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Customers customers) {
        List<Customers> list = customersService.selectCustomersList(customers);
        ExcelUtil<Customers> util = new ExcelUtil<Customers>(Customers.class);
        util.exportExcel(response, list, "客户数据");
    }
}
